﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using CancerPanleReport.core;
using Newtonsoft.Json.Linq;
using System.Drawing;

namespace CancerPanleReport.form
{
    public class BaseForm : Form
    {
        private static string COMBO_BOX_DISPLAY = "display";
        private static string COMBO_BOX_VALUE = "value";

        private static string TREE_PARENT_ID = "parent_id";

        private static string JSON = "json";

        private static string ID = "id";

        protected class CheckResult
        {
            public CheckResult CheckEmpty(Control ctrl, string msg)
            {
                if (Message == null)
                {
                    if (ctrl.Text.Length == 0)
                    {
                        Control = ctrl;
                        Message = msg;
                    }
                }
                return this;
            }

            public CheckResult CheckAction(Control ctrl, string msg, string action, IDictionary<string, object> args)
            {
                if (Message == null)
                {
                    object result = ActionControllerWrap.Instance.DoAction(action, args);
                    if (true)
                    {
                        Control = ctrl;
                        Message = msg;
                    }
                }
                return this;
            }

            public Control Control { get; set; }

            public string Message { get; set; }
        }

        protected void LoadComboBoxItem(ComboBox comboBox, string type, string display)
        {
            LoadComboBoxItem(comboBox, type, display, display);
        }

        protected void LoadComboBoxItem(ComboBox comboBox, string type, string display, string value)
        {
            IList<IDictionary<string, object>> data = LoadList(type);

            DataTable dt = new DataTable();
            dt.Columns.Add(COMBO_BOX_DISPLAY);
            dt.Columns.Add(COMBO_BOX_VALUE);
            foreach (IDictionary<string, object> e in data)
            {
                JObject json = JObject.Parse(e[JSON] as string);
                DataRow dr = dt.NewRow();
                dr[COMBO_BOX_DISPLAY] = json.Value<string>(display);
                dr[COMBO_BOX_VALUE] = json.Value<string>(value);
                dt.Rows.Add(dr);
            }

            comboBox.DataSource = dt;
            comboBox.DisplayMember = COMBO_BOX_DISPLAY;
            comboBox.ValueMember = COMBO_BOX_VALUE;
        }

        protected void LoadTreeViewItem(TreeView treeView, string type, string display)
        {
            IList<IDictionary<string, object>> data = LoadList(type);

            foreach (IDictionary<string, object> t in data)
            {
                object parentId = t[TREE_PARENT_ID];
                if (parentId != null)
                    continue;

                string strJson = t[JSON] as string;
                if (strJson == null)
                    continue;

                JObject obj = JObject.Parse(strJson);
                TreeNode childNode = treeView.Nodes.Add(obj.Value<string>(display));
                LoadTreeViewChildren(childNode, data, Convert.ToInt32(t[ID]), display);
            }
        }

        private void LoadTreeViewChildren(TreeNode node, IList<IDictionary<string, object>> data, int id, string display)
        {
            foreach (IDictionary<string, object> t in data)
            {

                object parentId = t[TREE_PARENT_ID];
                if (parentId == null)
                    continue;

                if (Convert.ToInt32(parentId) != id)
                    continue;

                string strJson = t[JSON] as string;
                if (strJson == null)
                    continue;

                JObject obj = JObject.Parse(strJson);

                TreeNode childNode = node.Nodes.Add(obj.Value<string>(display));

                LoadTreeViewChildren(childNode, data, Convert.ToInt32(t[ID]), display);

            }
        }

        protected void LoadMenuItem(MenuStrip menuStrip, string type, string display, string action)
        {
            IList<IDictionary<string, object>> data = LoadList(type);

            foreach (IDictionary<string, object> t in data)
            {
                object parentId = t[TREE_PARENT_ID];
                if (parentId != null)
                    continue;

                string strJson = t[JSON] as string;
                if (strJson == null)
                    continue;

                JObject obj = JObject.Parse(strJson) as JObject;

                ToolStripMenuItem child = new ToolStripMenuItem(obj.Value<string>(display));
                //child.Click += this.clickMenuItem;
                menuStrip.Items.Add(child);
                LoadMenuChildren(child, data, Convert.ToInt32(t[ID]), display, action);
            }
        }

        private void LoadMenuChildren(ToolStripMenuItem menuItem, IList<IDictionary<string, object>> data, int id, string display, string action)
        {
            foreach (IDictionary<string, object> t in data)
            {
                object parentId = t[TREE_PARENT_ID];
                if (parentId == null)
                    continue;

                if (Convert.ToInt32(parentId) != id)
                    continue;

                string strJson = t[JSON] as string;
                if (strJson == null)
                    continue;

                JObject obj = JObject.Parse(strJson) as JObject;

                ToolStripMenuItem child = new ToolStripMenuItem(obj.Value<string>(display));
                child.Click += this.ClickMenuItem;
                child.Tag = obj.Value<string>(action);
                menuItem.DropDownItems.Add(child);
                LoadMenuChildren(child, data, Convert.ToInt32(t[ID]), display, action);

            }
        }

        private void ClickMenuItem(object sender, EventArgs e)
        {
            if (sender is ToolStripItem)
            {
                ToolStripItem menuItem = sender as ToolStripItem;
                if (menuItem.Tag is string)
                {
                    string action = menuItem.Tag as string;
                    ActionControllerWrap.Instance.DoAction(action);
                }
            }
        }

        protected void LoadListViewItem(ListView listView, string type, string display, string action, string icon)
        {
            IList<IDictionary<string, object>> data = LoadList(type);

            listView.MouseDoubleClick += new MouseEventHandler(MouseDoubleClickListView);
            ImageList imageList = new ImageList();
            imageList.ImageSize = new Size(48, 48);
            listView.LargeImageList = imageList;

            foreach (IDictionary<string, object> e in data)
            {
                JObject obj = JObject.Parse(e[JSON] as string) as JObject;
                ListViewItem item = new ListViewItem(obj.Value<string>(display));

                string iconName = obj.Value<string>(icon);
                if (iconName != null)
                {

                    if (!imageList.Images.ContainsKey(iconName))
                        imageList.Images.Add(iconName, Image.FromFile(iconName));
                    item.ImageKey = iconName;
                }
                else
                {
                    item.ImageIndex = 0;
                }
                item.Tag = obj.Value<string>(action);
                listView.Items.Add(item);
            }
        }

        void MouseDoubleClickListView(object sender, MouseEventArgs e)
        {
            if (sender is ListView)
            {
                ListView listView = sender as ListView;
                ListViewHitTestInfo info = listView.HitTest(e.X, e.Y);
                if (info.Item != null)
                {
                    if (info.Item.Tag is string)
                    {
                        string action = info.Item.Tag as string;
                        object o = ActionControllerWrap.Instance.DoAction(action);
                    }
                }
            }
        }

        protected IList<IDictionary<string, object>> LoadList(string type)
        {
            Dictionary<string, object> actionParams = new Dictionary<string, object>();
            actionParams.Add("type", type);
            return ActionControllerWrap.Instance.DoAction("/tree_node/query.do", actionParams) as IList<IDictionary<string, object>>;
        }

        protected string LoadString(string key)
        {
            Dictionary<string, object> actionParams = new Dictionary<string, object>();
            actionParams.Add("key", key);
            return ActionControllerWrap.Instance.DoAction("/variable/byKey.do", actionParams) as string;
        }


        protected void LoadListViewItemListFiles(ListView listView, string dir)
        {
            string[] files = Directory.GetFiles(dir);

            listView.Clear();

            ImageList imageList = new ImageList();
            listView.SmallImageList = imageList;
            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(file);

                imageList.Images.Add(fileName, icon);
                listView.Items.Add(fileName, fileName).Tag = file;
            }

            listView.MouseDoubleClick += new MouseEventHandler(MouseDoubleClickListViewFile);

        }

        void MouseDoubleClickListViewFile(object sender, MouseEventArgs e)
        {
            if (sender is ListView)
            {
                ListView listView = sender as ListView;
                ListViewHitTestInfo info = listView.HitTest(e.X, e.Y);
                if (info.Item != null)
                {
                    if (info.Item.Tag is string)
                    {
                        string action = info.Item.Tag as string;

                        Process.Start(action);
                    }
                }
            }
        }

        protected CheckResult CheckEmpty(Control ctrl, string msg)
        {
            CheckResult checkResult = new CheckResult();
            return checkResult.CheckEmpty(ctrl, msg);
        }

    }
}
